/*!
 * Ext JS Library 3.3.0
 * Copyright(c) 2006-2010 Ext JS, Inc.
 * licensing@extjs.com
 * http://www.extjs.com/license
 */
Ext.onReady(function(){
		Ext.QuickTips.init();
		
		//insert into locations (ygid, ygxm, device, mobile, icon, color, lon_lat, update_at) 
		//values (1, 'admin', 'iphone', '18621361840', '', 'blue', '118.72049, 32.00371', TIMESTAMP '2011-06-14 23:06:19');
		//insert into locations (ygid, ygxm, device, mobile, icon, color, lon_lat, update_at) 
		//values (8, '朱媛媛', 'iphone', '13862615770', '', 'blue', '118.74813, 32.01244', TIMESTAMP '2011-06-13 23:06:19');
		
		var markers, popup, feature;

		var updateUser = function(markers) {
				var pars={};
				new Ajax.Request("/mobile/get_all_user", { 
					method: "POST",
					parameters: pars,
					onComplete:	 function(request) {
						var users = eval("("+request.responseText+")");
						for (var i = users.length - 1; i >= 0; i--){
							var loc = users[i].users;
							
							var size = new OpenLayers.Size(32,32);
							var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
							//var offset = new OpenLayers.Pixel(-16,-16);
							var icon = new OpenLayers.Icon('/images/mobile/'+loc.icon,size,offset);
							
							var pts = loc.lon_lat.split(" ");
							var x0 = parseFloat(pts[0]);
							var y0 = parseFloat(pts[1]);
							
							var marker = new OpenLayers.Marker(new OpenLayers.LonLat(x0, y0),icon); 
							markers.addMarker(marker);
							//marker.events.register("mousedown", marker, mousedown);
						};
						
					}
				});
				
		};

		/*
		var options = {
			projection: new OpenLayers.Projection('EPSG:900913'), units: "m", maxResolution: 152.87405654296876,
			maxExtent: new OpenLayers.Bounds(-2.003750834E7,-2.003750834E7,2.003750834E7,2.003750834E7) 
		};

		var map	 = new OpenLayers.Map($('map'), options);
		var gsat = new OpenLayers.Layer.Google("谷歌卫星图", {type: G_HYBRID_MAP, "sphericalMercator": true,	 opacity: 1, numZoomLevels: 20});
		var gmap = new OpenLayers.Layer.Google("谷歌地图", {type: G_NORMAL_MAP, "sphericalMercator": true,	 opacity: 1, numZoomLevels: 20});

		map.addLayers([gmap, gsat]);
		
		map.addControl(new OpenLayers.Control.LayerSwitcher());
		map.addControl(new OpenLayers.Control.MousePosition());
		
		var map_view = new Ext.Panel({
			//id : 'map',
			//title: '任务跟踪',
			//autoScroll: true,
			items: [{
				xtype: 'mapcomponent',
				map: map
			}],
			xtype:"panel",
			width:800,
			height:800,
			style:'margin:0px 0px',
			layout:'fit'
		});
		*/
		

		var options = {
			projection: new OpenLayers.Projection('EPSG:900913'), units: "m", maxResolution: 152.87405654296876,
			maxExtent: new OpenLayers.Bounds(-2.003750834E7,-2.003750834E7,2.003750834E7,2.003750834E7) 
		};
		
		map	 = new OpenLayers.Map($('map'), options);
		var gsat = new OpenLayers.Layer.Google("谷歌卫星图", {type: G_HYBRID_MAP, "sphericalMercator": true,	 opacity: 1, numZoomLevels: 20});
		var gmap = new OpenLayers.Layer.Google("谷歌地图", {type: G_NORMAL_MAP, "sphericalMercator": true,	 opacity: 1, numZoomLevels: 20});

		map.addLayers([gmap, gsat]);

		map.addControl(new OpenLayers.Control.LayerSwitcher());
		map.addControl(new OpenLayers.Control.MousePosition());

		map.setCenter(new OpenLayers.LonLat(13216458.8645921,3763329.0043342), 14);
		
		var markers = new OpenLayers.Layer.Markers( "Markers" );
		map.addLayer(markers);

		/*
		map	 = new OpenLayers.Map('map');
		var gsat = new OpenLayers.Layer.Google("谷歌卫星图", {type: G_HYBRID_MAP,	opacity: 1, numZoomLevels: 20});
		var gmap = new OpenLayers.Layer.Google("谷歌地图", {type: G_NORMAL_MAP,	 opacity: 1, numZoomLevels: 20});

		map.addLayers([gmap, gsat]);

		map.addControl(new OpenLayers.Control.LayerSwitcher());
		map.addControl(new OpenLayers.Control.MousePosition());

		
		markers = new OpenLayers.Layer.Markers( "Markers" );
		map.addLayers([vectors, markers]);

		map.setCenter(new OpenLayers.LonLat(118.72049, 32.00371), 14);
		
		var size = new OpenLayers.Size(32,32);
		var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
		var icon = new OpenLayers.Icon('/images/mobile/egg.png',size,offset);
		markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(118.72049, 32.00371),icon));
		*/
		
		vectors = new OpenLayers.Layer.Vector("路线", {
				isBaseLayer: false,
				styleMap: styles
		});
		map.addLayer(vectors);
		
		//diable ZoomWheel()
		var controls = map.getControlsByClass('OpenLayers.Control.Navigation');
		for(var i = 0; i<controls.length; ++i)
			controls[i].disableZoomWheel();
			
			
		updateUser(markers);

		map.events.register("click", map, function (e) {
			pt = this.getLonLatFromPixel(e.xy);
			
			var pars={lon:pt.lon, lat:pt.lat};
			new Ajax.Request("/mobile/query_user", { 
				method: "POST",
				parameters: pars,
				onComplete:	 function(request) {
					var users = eval("("+request.responseText+")");
					if (users.size() > 0) {
						locs = users[0].users;
						
						//innerHtml = locs.ygxm + "<br>电话：" + locs.mobile + "<br> 更新时间：" + locs.update_at + 
						//	'<input type="button" name="test" value="查看路线" onclick="inform(\''+locs.ygid+'\')" >';
						
						innerHtml = locs.ygxm + "<br>电话：" + locs.mobile + "<br> 更新时间：" + locs.update_at + 
							'<br><br><br><br><br><a class="tryitbtn" href="/mobile/tracks?ygid='+locs.ygid+'" target="_blank">轨迹</a>'
							
						popup = new OpenLayers.Popup("属性", 
												new OpenLayers.LonLat(pt.lon,pt.lat),
												new OpenLayers.Size(200,150),
												innerHtml,
												true);
						popup.closeOnMove = true;
						//popup.panMapIfOutOfView = true;
						popup.backgroundColor = 'lightgray';
						//popup.backgroundColor = '#98F5FF';
						popup.setOpacity(0.8);
						map.addPopup(popup);
					};
				}
			});
			Event.stop(e);
		});
});